//倒计时
var wait=60;
function time(o) {
    if (wait == 0) {
        o.removeAttribute("disabled");
        o.value="获取验证码";
        wait = 60;
    } else {

        o.setAttribute("disabled", true);
        o.value="重新发送(" + wait + ")";
        wait--;
        setTimeout(function() {
                time(o)
            },
            1000)
    }
}

function verify() {
    var userNameEmailPhone = $('#userNameEmailPhone').val();
    var password = $('#password').val();
    var confirm_password = $('#confirm_password').val();
    var checkChinese = /[\u4e00-\u9fa5]/g;

    if(userNameEmailPhone == ''){
        layer.msg('请输入用户名');
        return false
    }
    else if (checkChinese.test(userNameEmailPhone)) {
        layer.msg('用户名不能为汉字');
        return false
    }
    else if(userNameEmailPhone.length < 6 || userNameEmailPhone.length >20 ){
        layer.msg('用户名为6-20位字母数字');
        return false
    }
    else if(checkSymbol(userNameEmailPhone)){
        layer.msg('用户名不能包含特殊符号');
        return false
    }
    else if(password == ''){
        layer.msg('请输入密码');
        return false
    }
    else if(password.indexOf(" ") != -1){
        layer.msg('密码为6-16位数字、字符、字母');
        return false
    }
    else if(password.length < 6 || password.length >16 ){
        layer.msg('密码为6-16位数字、字符、字母');
        return false
    }
    else if(password != confirm_password){
        layer.msg('两次输入的密码不一致，请重输');
        return false
    }
    else{
        var data = {
            userNameEmailPhone: $('#userNameEmailPhone').val()
        };
        _AJAX({
            url:AJAX_URL_USER + '/api/user/login/userNameEmailPhoneIsRegister',
            type:'get',
            data: data,
            async: false,
            success: function (data) {
                if(!reqIsSuccess(data)) {
        			layer.msg('该登录名已经注册，请更换登录名');
                    return false;
                }
            }
        });
    }
    return true;
}

//获取动态码
function sendcode(obj){
    if(verify()){
        time(obj);
        _AJAX({
            url:AJAX_URL_USER + '/api/user/login/sendVerifyCode',
            type:'get',
            data: {
                userNameEmailPhone: $('#userNameEmailPhone').val(),
            },
            success: function (data) {
                if(reqIsSuccess(data)) {
        			layer.msg('验证码发送成功');
                }
            }
        });
    }
}

var registerFlag = true;

//注册
function register(){
    if(registerFlag == true){
        registerFlag = false;
        var data = {
            platformTypeEnum: 'NORMAL',
            'userName': $('#userName').val(),
            'password': $('#password').val(),
            'phoneNum': $('#phone').val(),
            'verifyCode': $('#verifycode').val()
        };
        _AJAX({
            url:AJAX_URL_USER + '/api/user/register',
            type:'post',
            data: data,
            success: function (data) {
                registerFlag = true;
                if(data.code == -2){
                    layer.alert(data.msg);
                }else if(data.code == 0){
                    //自动登录
                    _AJAX({
                        url:AJAX_URL_USER + '/api/auth/jwt/token',
                        type:'post',
                        contentType: 'application/json;charset=utf-8', //设置请求头信息
                        data: JSON.stringify({
                            'username': $('#userName').val(),
                            'password': $('#password').val(),
                            platformType: 'NORMAL'
                        }),
                        dataType:'JSON',
                        success: function (data) {
                            $.cookie('token',data.data,{path:"/"});
                            window.location.href = 'backmanage.html';
                        }
                    });
                }
            }
        });
    }
}

//表单验证
$.validator.setDefaults({
    submitHandler: function () {
        //验证通过后
        register();
    }
});

$().ready(function () {
    $.validator.addMethod("checkName", function (value) {
        var checkName = /[\u4e00-\u9fa5]/g;
        return !checkName.test(value);
    }, "用户名为6-20位字母数字");

    $("#commentForm").validate({
        rules: {
            userName: {
                required: true,
                checkName: true,
                minlength: 6,
                maxlength:20,
                specialCharFilter: '',
                remote:{
                    type:"get",
                    url:AJAX_URL_USER + '/api/user/userNameIsRegister'
                }
            },
            password: {
                required: true,
                minlength: 6,
                hpass: '',
                maxlength: 16
            },
            confirm_password: {
                required: true,
                minlength: 6,
                maxlength: 16,
                equalTo: "#password"
            },
            phone:{
                phone:'',
                remote:{
                    type:"get",
                    url:AJAX_URL_USER + '/api/user/phoneIsRegister'
                }
            },
            verifycode: {
                required: true
            }
        },
        messages: {
            userName: {
                required: "请输入用户名",
                minlength: "用户名为6-20位字母数字",
                maxlength: "用户名为6-20位字母数字",
                remote:"用户名已存在，请重输"
            },
            phone:{
                remote:"手机号已注册，请更换手机号"
            },
            password: {
                required: "请输入密码",
                minlength: "密码为6-16位数字、字符、字母",
                maxlength: "密码为6-16位数字、字符、字母"
            },
            confirm_password: {
                required: "请输入密码",
                minlength: "密码为6-16位数字、字符、字母",
                maxlength: "密码为6-16位数字、字符、字母",
                equalTo: "两次输入的密码不一致，请重输"
            },
            verifycode: {
                required: "请输入动态码"
            }
        }
    })
});

// 自定义电话验证
jQuery.validator.addMethod("phone", function(value, element) {
    if(!(/^1[34578]\d{9}$/.test(value))){
        return false;
    }else{
        return true;
    }
}, "请正确填写您的手机号");

// 自定义密码验证
jQuery.validator.addMethod("hpass", function(value, element) {
    if(value.indexOf(" ") != -1){
        return false;
    }else{
        return true;
    }
}, "密码为6-16位数字、字符、字母");

jQuery.validator.addMethod("specialCharFilter", function(value, element) {
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;,.<>/?~！@#￥……&*（）——|【】‘；：”“'。，、？%+ 　\"\\\\]");
    var specialStr = "";
    for(var i=0;i<value.length;i++){
        specialStr += value.substr(i, 1).replace(pattern, '');
    }

    if( specialStr == value){
        return true;
    }
},"用户名为6-20位字母数字");